package server.messageContent;

import server.AccountServiceImpl;
import common.CurrencyEnum;
import common.SerializationManager;
import common.ServiceErrorException;
import common.ServiceResponseEnum;
import common.data.ResponseTypeEnum;
import common.interfaces.MessageContent;

public class TransferFundReplyMsgContent implements MessageContent {
	private byte[] data;
	private int length;
	

	public TransferFundReplyMsgContent(ServiceResponseEnum response, long accIdSender, long accIdReceiver, float amount, CurrencyEnum currency) throws ServiceErrorException {
		String responseString = null;
		ResponseTypeEnum responseTypeEnum;
		switch (response) {
				
			case SUCCESS_TRANSFER_FUND:
				responseString = response.getMsg() +"from account "+ String.valueOf(accIdSender)+" to account "+
			    String.valueOf(accIdReceiver)  +" amount " + currency.getMsg() + amount ;	
				responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
				break;

		case NON_EXISTING_SENDER_ACCOUNT:
			responseString = response.getMsg();
			responseTypeEnum = ResponseTypeEnum.ERROR;

			break;

		case NON_EXISTING_RECIPIENT_ACCOUNT:
			responseString = response.getMsg();
			responseTypeEnum = ResponseTypeEnum.ERROR;
			break;
		case SENDER_ACCOUNT_WRONG_PASSWORD:
			responseString = response.getMsg();
			responseTypeEnum = ResponseTypeEnum.ERROR;
			break;
		case INSUFFICIENT_SENDER_BALANCE:
			responseString = response.getMsg();
			responseTypeEnum = ResponseTypeEnum.ERROR;
			break;
		default:
			responseTypeEnum = ResponseTypeEnum.ERROR;
			break;
		}
		byte[] responseTypeEnumStream = SerializationManager.getCurrent()
				.fromResponseTypeEnum(responseTypeEnum);

		byte[] responseStream = SerializationManager.getCurrent().fromString(
				responseString);
		byte[] responseLengthStream = SerializationManager.getCurrent()
				.fromInt(responseStream.length);
		int currentIndex = 0;

		if (responseTypeEnum == ResponseTypeEnum.ERROR) {

			this.data = new byte[responseTypeEnumStream.length
					+ responseLengthStream.length + responseStream.length];
			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex,
					responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;

			System.arraycopy(responseLengthStream, 0, data, currentIndex,
					responseLengthStream.length);
			currentIndex += responseLengthStream.length;

			System.arraycopy(responseStream, 0, data, currentIndex,
					responseStream.length);
		} else { // response object
			byte[] responseObjectStream = SerializationManager.getCurrent()
					.fromFloat(
							AccountServiceImpl.getCurrent().BalanceEnquiry(
									accIdSender));
			byte[] responseObjectLengthStream = SerializationManager
					.getCurrent().fromInt(responseObjectStream.length);

			data = new byte[responseTypeEnumStream.length
					+ responseLengthStream.length 
					+ responseStream.length
					+ responseObjectLengthStream.length
					+ responseObjectStream.length];

			System.arraycopy(responseTypeEnumStream, 0, data, currentIndex,
					responseTypeEnumStream.length);
			currentIndex += responseTypeEnumStream.length;

			System.arraycopy(responseLengthStream, 0, data, currentIndex,
					responseLengthStream.length);
			currentIndex += responseLengthStream.length;

			System.arraycopy(responseStream, 0, data, currentIndex,
					responseStream.length);
			currentIndex += responseStream.length;

			System.arraycopy(responseObjectLengthStream, 0, data, currentIndex,
					responseObjectLengthStream.length);
			currentIndex += responseObjectLengthStream.length;

			System.arraycopy(responseObjectStream, 0, data, currentIndex,
					responseObjectStream.length);
			currentIndex += responseObjectStream.length;
		}
	}

	@Override
	public byte[] getByte() {
		return this.data;
	}

	@Override
	public int getLength() {
		return this.data.length;
	}
}
